﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;

#region "Custom namespace declarations"
using Comkarl;
using Comkarl.Architecture.Patterns.MVC;
#endregion

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Represents the base type for any entity in the paradigm.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the indentifier's name.
    /// </typeparam>
    public abstract class Entity<TIdentifierName> : MVCMember<TIdentifierName>, IEquatable<Entity<TIdentifierName>>, IIdentifiable<EntityIdentifier<TIdentifierName>, TIdentifierName> 
        where TIdentifierName : IEquatable<TIdentifierName>
    {
        #region "Fields"
        /// <summary>
        /// Retrieves the unique identifier for the current entity.
        /// </summary>
        private readonly EntityIdentifier<TIdentifierName> id;
        #endregion

        #region "Constructors"
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="id">
        /// The identifier for the entity.
        /// </param>
        public Entity(EntityIdentifier<TIdentifierName> id)
            : base(id)
        {
            EntityIdentifier<TIdentifierName>.Validate(id);
            this.id = id;
        }

        /// <summary>
        /// Internally called.
        /// </summary>
        private Entity()
            : base(null)
        {
        }
        #endregion

        #region "Properties"
        /// <summary>
        /// Gets the current entity's unique identifier.
        /// </summary>
        public EntityIdentifier<TIdentifierName> ID
        {
            get
            {
                return id;
            }
        }
        #endregion

        #region "Overriden methods"
        public override bool Equals(object obj)
        {
            return ((View<TIdentifierName>)obj).ID.Name.ToString() == ID.Name.ToString();
        }
        #endregion

        #region IEquatable<Entity<TIdentifierName>> Members

        public bool Equals(Entity<TIdentifierName> other)
        {
            return ID == other.ID;
        }

        #endregion
    }
}
